package com.atguigu.domain;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;

/**
 * 购物车类
 */
public class Cart implements Serializable {
    /**
     * 存放商品数据
     */
    private Map<Integer,BookItems> items = new LinkedHashMap<>();

    private int totalCount;

    private BigDecimal totalAllMoney;

    public int getTotalCount() {
        int count = 0;
        for (Integer integer : items.keySet()) {
            BookItems bookItems = items.get(integer);
            count += bookItems.getCount();
        }
        return count;
    }


    public BigDecimal getTotalAllMoney() {
        BigDecimal bigDecimal = new BigDecimal("0");
        for (Integer integer : items.keySet()) {
            BookItems bookItems = items.get(integer);
            bigDecimal = bigDecimal.add(bookItems.getTotalMoney());
        }
        return bigDecimal;
    }



    public void add(Integer id , BookItems bookItems) {
        items.put(id,bookItems);
    }

    public void delete(Integer id) {
        items.remove(id);
    }

    public void update(Integer id, Integer count) {
        BookItems bookItems = items.get(id);
        bookItems.setCount(count);
    }

    public List<BookItems> getItems() {
        Collection<BookItems> values = items.values();
        List<BookItems> bookItems = new ArrayList<>(values);
        return bookItems;
    }

    public void clear() {
        items.clear();
    }

    public BookItems get(Integer id) {
        return items.get(id);
    }


}
